package bean;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import org.primefaces.model.UploadedFile;

import Model.HoaDon;
import Model.NhaSanXuat;
import Model.SanPham;
import Util.DAO;
import Util.SanPhamDataModel;
import Util.Util;
@ManagedBean

public class BeanQuanLy implements Serializable{
	public MenuModel model;
	public static String tieude;
	public SanPham selectedSanPham;
	private static List<SanPham> list;
	private static SanPhamDataModel dataModel;
	private String tenhang;
	public SanPhamDataModel getDataModel() {
		return dataModel;
	}
	public void setDataModel(SanPhamDataModel dataModel) {
		this.dataModel = dataModel;
	}
	
	public List<SanPham> getList() {
		return list;
	}
	public void setList(List<SanPham> list) {
		BeanQuanLy.list = list;
	}
	public SanPham getSelectedSanPham() {
		return selectedSanPham;
	}
	public void setSelectedSanPham(SanPham selectedSanPham) {
		this.selectedSanPham = selectedSanPham;
	}
	public String getTieude() {
		return tieude;
	}
	public void setTieude(String tieude) {
		System.out.println("da set tieu de");
		this.tieude = tieude;
	}
	static{
		tieude = "Tất Cả Sản Phẩm";
		list = DAO.laytatcasanpham();
		dataModel = new SanPhamDataModel(list);
	}
	public BeanQuanLy(){
		FacesContext facesCtx = FacesContext.getCurrentInstance();
		ELContext elCtx = facesCtx.getELContext();
		ExpressionFactory expFact = facesCtx.getApplication().getExpressionFactory();
		
		model = new DefaultMenuModel();
		Submenu sub = new Submenu();
		sub.setLabel("Hãng Sản Xuất");
		
		MenuItem itemall = new MenuItem();
		itemall.setTarget(tieude);
		itemall.setUpdate(":form:bangsanpham");
		itemall.setValue("Tất Cả Sản Phẩm");
		itemall.setAjax(true);
		itemall.setActionExpression(expFact.createMethodExpression(elCtx, "#{beanQuanLy.hang(\"tatca\")}", String.class, new Class[0]));
		sub.getChildren().add(itemall);
		List<NhaSanXuat> lnsx = DAO.laytatcanhasanxuat();
		for (NhaSanXuat nhaSanXuat : lnsx) {
			MenuItem item = new MenuItem();
			String hang = nhaSanXuat.getTennhasanxuat();
			
			item.setValue(hang);
			
			item.setUpdate(":form:bangsanpham");
			item.setAjax(true);
			item.setActionExpression(expFact.createMethodExpression(elCtx, "#{beanQuanLy.hang(\""+hang+"\")}", String.class, new Class[0]));
			sub.getChildren().add(item);
	        }
		model.addSubmenu(sub);
		
		FacesContext context = FacesContext.getCurrentInstance();
        Map<String, String> paramMap = context.getExternalContext().getRequestParameterMap();
        String reset = paramMap.get("reset");
		
		if(reset != null && reset.equals("true")){
			tieude = "Tất Cả Sản Phẩm";
			list = DAO.laytatcasanpham();
			dataModel = new SanPhamDataModel(list);
		}
		
	}
	
	public void hang(String tenhang){
		if(tenhang.equals("tatca")){
			tieude = "Tất Cả Sản Phẩm";
			list = DAO.laytatcasanpham();
			dataModel = new SanPhamDataModel(list);
			return;
		}
		tieude = tenhang;
		list = DAO.timkiemtheohang(tenhang);
		dataModel = new SanPhamDataModel(list);
		
	}
	
	public MenuModel getModel() {
		return model;
	}
	
	 public String xoasanpham(){
		 System.out.println("delete");
			DAO.xoasanpham(selectedSanPham);
			list.remove(selectedSanPham);
			return "quanlysanpham?faces-redirect=true&amp";
		}
	 
	 public boolean kiemtrahoadon(SanPham sanPham){
		 Set<HoaDon> dshd = sanPham.getDshd();
 			for (HoaDon hoaDon : dshd) {
				if(!hoaDon.isDathanhtoan()){
					 return true;
				}
			}
 			return false;
		}
	
}
	

